package manage.commons.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import manage.commons.Constants;

import org.apache.commons.io.FileUtils;

public class UploadUtils {

	public static String upload(String savePath, byte[] bytes, String ext) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM", Locale.ENGLISH);
		String yearMonth = df.format(new Date());
		String folder = savePath + yearMonth + Constants.SEPARATE;
		new File(folder);
		UUID uid = UUID.randomUUID();
		String name = uid.toString().replaceAll("-", "_") + "." + ext;
		File file = new File(folder, name);
		try {
			FileUtils.writeByteArrayToFile(file, bytes);
			return yearMonth + "-" + name;
		} catch (IOException e) {
			return "";
		}
	}

	public static void delete(String fileName) {
		File file = new File(fileName);
		if (file != null && file.isFile()) {
			file.delete();
		}
	}
}
